using ERP.Inventory.Api.Read.Application.Command.库存管理;
using ERP.Inventory.Api.Read.DTO;
using ERP.Inventory.Domain.库存管理;
using ERP.Inventory.Errorcode;
using ERP.Inventory.Infastructure.Interface;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.IO.Pipelines;
using System.Security.Cryptography.X509Certificates;

namespace ERP.Inventory.Api.Read.Application.Handler.库存管理
{
    public class GetcssHandler : IRequestHandler<GetcssCommand, ApiPaging<cssDTO>>
    {
        private readonly IBaseRepository<CheckandShopModel> checkshopRepo;
        private readonly IBaseRepository<ShopModel> shopRepo;
        private readonly IBaseRepository<StoreModel> storeRepo;

        public GetcssHandler(IBaseRepository<CheckandShopModel> checkshopRepo, IBaseRepository<ShopModel> shopRepo, IBaseRepository<StoreModel> storeRepo)
        {

            this.checkshopRepo = checkshopRepo;
            this.shopRepo = shopRepo;
            this.storeRepo = storeRepo;
        }
        public Task<ApiPaging<cssDTO>> Handle(GetcssCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiPaging<cssDTO>();
            
            try
            {
                // 确保请求参数有效
                if (request.Id <= 0)
                {
                    res.Message = "无效的盘点ID";
                    res.Code = ApiEnum.Error;
                    return Task.FromResult(res);
                }
                
                var cs = checkshopRepo.GetAll().Where(x => x.InvenId == request.Id);
                var shop = shopRepo.GetAll();
                var store = storeRepo.GetAll();
                
                var linq = (from a in cs
                            join b in shop on a.ShopId equals b.Id
                            join c in store on b.StoreId equals c.Id
                            select new cssDTO
                            {
                                Id = a.Id,
                                InvenId = a.InvenId,
                                ShopId = a.ShopId,
                                ShopName = b.ShopName,
                                ShopCode = b.ShopCode,
                                ShopGui = b.ShopGui,
                                ShopUnit = b.ShopUnit,
                                SystemNum = b.SystemNum,
                                ActualNum = b.ActualNum,
                                DifferentCount = b.DifferentCount,
                                StoreId = b.StoreId,
                                StoreName = c.StoreName
                            });
            
                // 检查查询结果是否为空
                if (!linq.Any())
                {
                    res.Message = "未找到相关数据";
                    res.Code = ApiEnum.Ok;
                    res.totalcount = 0;
                    res.totalpage = 0;
                    res.Data = new List<cssDTO>();
                    return Task.FromResult(res);
                }
                
                var totalcount = linq.Count();
                var totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.PageSize);
                var data = linq.OrderByDescending(x => x.Id)
                              .Skip((request.PageIndex - 1) * request.PageSize)
                              .Take(request.PageSize)
                              .ToList();
                
                res.totalcount = totalcount;
                res.totalpage = totalpage;
                res.Message = "显示成功";
                res.Data = data;
                res.Code = ApiEnum.Ok;
            }
            catch (Exception ex)
            {
                res.Message = "查询失败: " + ex.Message;
                res.Code = ApiEnum.Error;
                res.Data = new List<cssDTO>();
            }
            
            return Task.FromResult(res);
        }
    }
}
